package com.cmft.generator.db.tool;

import com.cmft.generator.db.entity.TableConfig;
import com.cmft.generator.utils.BeanUtils;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;
import java.sql.Types;
import java.util.HashMap;

public class DBTool {
    private final static HashMap<Integer, String> commonMap = new HashMap<>();
    private final static HashMap<String, String> commonMap2 = new HashMap<>();


    public static Connection getConn(TableConfig tableConfig) {
        try {
            init();
            Class.forName(tableConfig.getDriverName());// 指定连接类型
            return DriverManager.getConnection(tableConfig.getUrl(), tableConfig.getUser(), tableConfig.getPassword());// 获取连接
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public static void close(Connection conn) {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {

        }
    }

    public static void close(Statement stmt) {
        try {
            if (stmt != null) {
                stmt.close();
            }
        } catch (Exception e) {

        }
    }

//    public void getTableInfo(String schema, String tableName) throws Exception {
//        DatabaseMetaData dbmd = conn.getMetaData();
//        ResultSet rs = dbmd.getColumns(null, schema, tableName, "%");
//        int count = rs.getMetaData().getColumnCount();
////		for (int i = 1; i < count; i++) {
////			System.out.println(rs.getMetaData().getColumnLabel(i));
////		}
//        System.out.println("-------------------------------------------------");
//        while (rs.next()) {
//            System.out.println(getBeanDefine(rs.getString("COLUMN_NAME"), rs.getInt("DATA_TYPE"), rs.getString("REMARKS")));
//        }
//    }


//	public ResultSet exeSql(String sql) throws Exception {
//		pst = conn.prepareStatement(sql);// 准备执行语句
//		return pst.executeQuery();
//	}


    public static String columnTypeToBeanType(int columnType) {
        String result = commonMap.get(columnType);
        if (result == null)
            throw new RuntimeException("not mitch type :" + columnType);
        return result;
    }

    public static String columnTypeToBeanType(String columnType) {
        String result = commonMap2.get(columnType.toUpperCase());
        if (result == null)
            throw new RuntimeException("not mitch type :" + columnType);
        return result;
    }

    public static void init() {
        commonMap.clear();
        commonMap.put(Types.BIGINT, long.class.getSimpleName());
        commonMap.put(Types.INTEGER, int.class.getSimpleName());
        commonMap.put(Types.SMALLINT, int.class.getSimpleName());
        commonMap.put(Types.TINYINT, int.class.getSimpleName());
        commonMap.put(Types.NUMERIC, int.class.getSimpleName());
        commonMap.put(Types.VARCHAR, String.class.getSimpleName());
        commonMap.put(Types.CHAR, String.class.getSimpleName());
        commonMap.put(Types.DECIMAL, BigDecimal.class.getSimpleName());
        commonMap.put(Types.DATE, java.util.Date.class.getSimpleName());
        commonMap.put(Types.TIMESTAMP, java.util.Date.class.getSimpleName());

        commonMap2.clear();
        commonMap2.put("BIGINT", long.class.getSimpleName());
        commonMap2.put("INTEGER", int.class.getSimpleName());
        commonMap2.put("SMALLINT", int.class.getSimpleName());
        commonMap2.put("TINYINT", int.class.getSimpleName());
        commonMap2.put("NUMERIC", int.class.getSimpleName());
        commonMap2.put("NUMBER", int.class.getSimpleName());
        commonMap2.put("TEXT", String.class.getSimpleName());
        commonMap2.put("CLOB", String.class.getSimpleName());
        commonMap2.put("VARCHAR", String.class.getSimpleName());
        commonMap2.put("VARCHAR2", String.class.getSimpleName());
        commonMap2.put("NVARCHAR", String.class.getSimpleName());
        commonMap2.put("NVARCHAR2", String.class.getSimpleName());
        commonMap2.put("CHAR", String.class.getSimpleName());
        commonMap2.put("NCHAR", String.class.getSimpleName());
        commonMap2.put("DECIMAL", BigDecimal.class.getSimpleName());
        commonMap2.put("DATE", java.util.Date.class.getSimpleName());
        commonMap2.put("TIMESTAMP", java.util.Date.class.getSimpleName());
    }

    public static final String tuoFeng(String str) {
        return BeanUtils.tuoFeng(str);
    }
}
